﻿
using NetworkPower.DataModel.Models;
using NPOI.HSSF.UserModel;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace NetworkPower.UI
{
    /// <summary>
    /// Interaction logic for InportAndExport.xaml
    /// </summary>
    public partial class ImportAndExport : Window
    {
        static NetworkPowerContext context = new NetworkPowerContext();

        public ImportAndExport()
        {
            InitializeComponent();
        }

        private void BtnImport_Click(object sender, RoutedEventArgs e)
        {
            //FolderBrowserDialog fbd = new FolderBrowserDialog();
            //var rs = fbd.ShowDialog();
            //if (rs == System.Windows.Forms.DialogResult.OK)
            //{
            //    string[] files = Directory.GetFiles(fbd.SelectedPath);
            //    foreach (string file in files)
            //    {
            //        string fileName = System.IO.Path.GetFileName(file);
            //        if (!isExistLineProtectBox(fileName) && (System.IO.Path.GetExtension(fileName).Equals(".xls") || System.IO.Path.GetExtension(fileName).Equals(".xlsx")))
            //        {
            //            GetFromExcelFile(file);
            //        }
            //    }
            //    System.Windows.Forms.MessageBox.Show("完成保存！");
            //}
        }


        private bool isExistLineProtectBox(string fileName)
        {
            //return context.Set<LineProtectBox>().Any(a => a.LineProtectBoxName.Equals(fileName));
            return false;
        }

        public IWorkbook GetWorkBook(string filePath)
        {
            IWorkbook wk = null;
            string extension = System.IO.Path.GetExtension(filePath);
            using (FileStream fs = File.OpenRead(filePath))
            {
                if (extension.Equals(".xls"))
                {
                    wk = new HSSFWorkbook(fs);
                }
                else
                {
                    wk = new XSSFWorkbook(fs);
                }
            }
            return wk;
        }

        //private LineProtectBox SaveLineProtectBox(string filePath)
        //{
        //    string fileName = System.IO.Path.GetFileName(filePath);
        //    var lineProtectBox = new LineProtectBox()
        //    {
        //        LineProtectBoxId = Guid.NewGuid(),
        //        LineProtectBoxName = fileName
        //    };
        //    context.LineProtectBoxs.Add(lineProtectBox);
        //    context.SaveChanges();
        //    return context.LineProtectBoxs.Where(s => s.LineProtectBoxId == lineProtectBox.LineProtectBoxId).FirstOrDefault();
        //}

        //private Department SaveDepartment(string sheetName, int sheetIndex, Guid LineProtectBoxId)
        //{
        //    var depart = new Department()
        //    {
        //        DepartmentId = Guid.NewGuid(),
        //        DepartmentName = sheetName,
        //        LineProtectBoxId = LineProtectBoxId,
        //        SheetIndex = sheetIndex
        //    };
        //    context.Departments.Add(depart);
        //    context.SaveChanges();
        //    return context.Departments.Where(s => s.DepartmentId == depart.DepartmentId).FirstOrDefault();
        //}

        public void GetFromExcelFile(string filePath)
        {
            IWorkbook wk = null;
            int rowIndex = 0;
            try
            {
                //var lineProtectBox = SaveLineProtectBox(filePath);
                //if (lineProtectBox == null)
                //    return;

                wk = GetWorkBook(filePath);
                for (int i = 0; i < wk.NumberOfSheets; i++)
                {
                    ISheet sheet = wk.GetSheetAt(i);
                    //var loopLine = SaveDepartment(sheet.SheetName, i, lineProtectBox.LineProtectBoxId);
                    //if (loopLine == null)
                    //{
                    //    return;
                    //}

                    IRow row = sheet.GetRow(0);
                    short PreviousColor = -1;
                    Guid PreviousGuid = Guid.Empty;
                    for (int j = 1; j <= sheet.LastRowNum; j++)
                    {
                        rowIndex++;
                        row = sheet.GetRow(j);
                        if (row != null)
                        {
                            //LoopLine pc = new LoopLine();
                            //pc.RowIndex = rowIndex;
                            //pc.LoopLineId = Guid.NewGuid();
                            //pc.DepartmentId = loopLine.DepartmentId;
                            //pc.LoopLineName = row.GetCell(0).ToString();
                            //pc.LoopLineNameColor = Common.ColorMapping.GetColor(row.GetCell(0).CellStyle.FillForegroundColor);
                            //pc.InternalLine = row.GetCell(1).ToString();
                            //pc.TerminalNo = row.GetCell(2).ToString();
                            //pc.TerminalNoColor = Common.ColorMapping.GetColor(row.GetCell(2).CellStyle.FillForegroundColor);
                            //pc.OuterLine = row.GetCell(3).ToString();
                            //pc.LoopNo = row.GetCell(4) == null ? string.Empty : row.GetCell(4).ToString();
                            //pc.LineProtextId = lineProtectBox.LineProtectBoxId;
                            //if (PreviousColor == row.GetCell(2).CellStyle.FillForegroundColor)
                            //{
                            //    pc.ShortConnectTo = PreviousGuid;
                            //    PreviousColor = -1;
                            //}
                            //else
                            //{
                            //    PreviousGuid = pc.LoopLineId;
                            //    PreviousColor = row.GetCell(2).CellStyle.FillForegroundColor;
                            //}
                            //context.LoopLines.Add(pc);
                        }
                    }
                    context.SaveChanges();
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }
        }

        private void btnExport_Click(object sender, RoutedEventArgs e)
        {
            new ExportList().ShowDialog();
        }

    }
}
